package com.skycong.hello77.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

import com.skycong.hello77.R;

/**
 * Created by Kiki on 2018/6/20.
 */

public class CustomView extends View {

    /**
     * 文本
     */
    private String titleText;
    /**
     * 文本颜色
     */
    private int titleTextColor;
    /**
     * 文本大小
     */
    private int titleTextSize;
    /**
     * 绘制时控制文本绘制的范围
     */
    private Rect bound;
    private Paint paint;

    public CustomView(Context context) {
        this(context,null);
    }

    public CustomView(Context context,AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomView(Context context,AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        /**
         * 获得我们所自定的属性
         */
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView,defStyleAttr,0);
        int n = a.getIndexCount();
        for(int i = 0;i<n;i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.CustomTitleView_titleText:
                    titleText = a.getString(attr);
                    break;
                case R.styleable.CustomTitleView_titleTextColor:
                    //默认颜色设置为黑色
                    titleTextColor = a.getColor(attr, Color.BLACK);
                    break;
                case R.styleable.CustomTitleView_titleTextSize:
                    //默认设置为16dp,TypeValue也可以把sp转化为px
                    titleTextSize = a.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,16,getResources().getDisplayMetrics()));
                    break;
            }
        }
        a.recycle();
        /**
         * 获得绘制文本的宽和高
         */
        paint = new Paint();
        paint.setTextSize(titleTextSize);
        bound = new Rect();
        paint.getTextBounds(titleText,0,titleText.length(),bound);

    }

    //系统帮我们测量的高度和宽度都是MATCH_PARNET，当我们设置明确的宽度和高度时
    // ，系统帮我们测量的结果就是我们设置的结果，
    // 当我们设置为WRAP_CONTENT,或者MATCH_PARENT系统帮我们测量的结果就是MATCH_PARENT的长度。
    //所以需要重写

    //EXACTLY：一般是设置了明确的值或者是MATCH_PARENT

    //AT_MOST：表示子布局限制在一个最大值内，一般为WARP_CONTENT

   // UNSPECIFIED：表示子布局想要多大就多大，很少使用
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height ;
        if (widthMode == MeasureSpec.EXACTLY)
        {
            width = widthSize;
        } else
        {
            paint.setTextSize(titleTextSize);
            paint.getTextBounds(titleText, 0, titleText.length(), bound);
            float textWidth = bound.width();
            int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
            width = desired;
        }

        if (heightMode == MeasureSpec.EXACTLY)
        {
            height = heightSize;
        } else
        {
            paint.setTextSize(titleTextSize);
            paint.getTextBounds(titleText, 0, titleText.length(), bound);
            float textHeight = bound.height();
            int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
            height = desired;
        }



        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
      paint.setColor(Color.YELLOW);
      canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint);
      paint.setColor(titleTextColor);
      canvas.drawText(titleText,getWidth()/2-bound.width()/2,getHeight()/2+bound.height()/2,paint);
    }
}
